package com.enjoy.server;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.cloudera.htrace.Trace;

import java.io.IOException;

public class HBaseBasic {

//    public static Configuration configuration;
//    static {
//        configuration = new Configuration();//HBaseConfiguration.create();
////        configuration.set("hbase.zookeeper.property.clientPort", "2181");
//        configuration.set("hbase.zookeeper.quorum", "114.215.153.225");//,114.215.154.105,121.42.148.100");
////        configuration.set("hbase.master", "114.215.153.225:9000");
//    }

    private static HBaseConfiguration hbaseConfig=null;
    static {
        Configuration config=new Configuration();
//        config.addResource("D:\\project\\distributedSearch\\search\\src\\main\\resource\\spring\\hbase-default.xml");
        config.set("hbase.zookeeper.quorum","10.164.27.39,10.251.23.44,10.164.0.145");
        config.set("hbase.rootdir","hdfs://10.164.27.39:9000/hbase");
        config.set("hbase.zookeeper.property.clientPort", "2181");
        hbaseConfig = new HBaseConfiguration(config);
    }

    public static void main(String[] args) {
        try {
            Trace trace = new Trace();
            creatTable("test", "aa,bb,cc".split(","));
            QueryAll("wujintao");
        }catch (Exception e){
            e.printStackTrace();
        }
        // QueryByCondition1("wujintao");
    }

    public static void creatTable(String tableName, String[] family)
            throws Exception {
        HBaseAdmin admin = new HBaseAdmin(hbaseConfig);
        HTableDescriptor desc = new HTableDescriptor(tableName);
        for (int i = 0; i < family.length; i++) {
            desc.addFamily(new HColumnDescriptor(family[i]));
        }
        if (admin.tableExists(tableName)) {
            System.out.println("table Exists!");
            System.exit(0);
        } else {
            admin.createTable(desc);
            System.out.println("create table Success!");
        }
    }

    /**
     * 查询所有数据
     * @param tableName
     */
    public static void QueryAll(String tableName) {
        try {
//        HTablePool pool = new HTablePool(configuration, 1000);
//        HTable table = (HTable) pool.getTable("CrawlTest6_webpage");
//            ResultScanner rs = table.getScanner(new Scan());
            HTable table = new HTable(hbaseConfig, Bytes.toBytes("CrawlTest6_webpage"));
            ResultScanner scanner =  table.getScanner(new Scan());
//            Result rs = scanner.next();
//            rs.g
            for (Result r : scanner) {
                System.out.println("获得到rowkey:" + new String(r.getRow()));
                for (KeyValue keyValue : r.raw()) {
                    System.out.println("列：" + new String(keyValue.getFamily())
                            + "====值:" + new String(keyValue.getValue()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 单条件查询,根据rowkey查询唯一一条记录
     * @param tableName
     */
    public static void QueryByCondition1(String tableName) {

        HTablePool pool = new HTablePool(hbaseConfig, 1000);
        HTable table = (HTable) pool.getTable(tableName);
        try {
            Get scan = new Get("abcdef".getBytes());// 根据rowkey查询
            Result r = table.get(scan);
            System.out.println("获得到rowkey:" + new String(r.getRow()));
            for (KeyValue keyValue : r.raw()) {
                System.out.println("列：" + new String(keyValue.getFamily())
                        + "====值:" + new String(keyValue.getValue()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}